package middleware

import (
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
	"mine-info-server/global"
	"mine-info-server/utils"
	"time"
)

// RecordOnlineUsers 记录在线用户数
func RecordOnlineUsers() gin.HandlerFunc {
	return func(c *gin.Context) {
		if "/manage-api/v1/adminUser/logout" == c.Request.RequestURI || "/manage-api/v1/adminUser/login" == c.Request.RequestURI {
			c.Next()
			return
		}
		// 记录在线人数
		global.GVA_REDIS.ZAdd(c, "online_users", &redis.Z{
			Score:  float64(time.Now().Unix()),
			Member: utils.GetUserID(c),
		})
		c.Next()
	}
}
